<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
      class="formSearch"
    >
      <el-form-item label="日期:">
        <el-date-picker
          size="mini"
          v-model="dataForm.time"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
        />
      </el-form-item>
      <el-form-item label="流量计:">
        <el-select
          placeholder="请选择流量计"
          v-model="dataForm.areaId"
          filterable
          clearable
          size="mini"
        >
          <el-option
            v-for="item in stationOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="getDataList(1)">查询</el-button>
        <el-button
          type="danger"
          size="small"
          v-if="isAuth('cms:tbCopyFlowData:delete')"
          @click="deleteHandle()"
          >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      height="73vh"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      />
      <el-table-column
        prop="id"
        width="100"
        header-align="center"
        align="center"
        label="id"
      />
      <el-table-column
        prop="area"
        width="140"
        header-align="center"
        align="center"
        label="流量计名称"
      />
      <el-table-column
        width="160"
        prop="date"
        header-align="center"
        align="center"
        label="更新时间"
      />
      <el-table-column
        prop="positiveFlow"
        header-align="center"
        align="center"
        label="正流量行度"
      />
      <el-table-column
        prop="negativeFlow"
        header-align="center"
        align="center"
        label="负流量行度"
      />
      <el-table-column
        prop="instantFlow"
        header-align="center"
        align="center"
        label="瞬时流量"
      />
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        show-overflow-tooltip
        label="备注"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="isAuth('cms:tbCopyFlowData:update')"
            @click="addOrUpdateHandle(scope.row.id)"
            >编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            v-if="isAuth('cms:tbCopyFlowData:delete')"
            @click="deleteHandle(scope.row.id)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      style="margin: 15px 0;"
      layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import AddOrUpdate from "./copyData-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        areaId: null,
        time: []
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      stationOptions: []
    };
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getStationOption();
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList(type) {
      this.dataListLoading = true;
      if (type === 1) {
        this.pageIndex = 1;
      }
      this.$http({
        url: this.$http.adornUrl("/cms/tbCopyFlowData/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          startDate: this.dataForm.time[0],
          endDate: this.dataForm.time[1],
          areaId: this.dataForm.areaId
        })
      })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
            this.dataListLoading = false;
          } else {
            this.dataList = [];
            this.totalPage = 0;
            this.dataListLoading = false;
          }
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `是否确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/cms/tbCopyFlowData/delete"),
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 获取泵站信息
    getStationOption() {
      this.$http({
        url: this.$http.adornUrl(`/cms/tbEquipment/combo`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stationOptions = data.list;
        } else {
          this.stationOptions = [];
        }
      });
    }
  }
};
</script>
<style lang="scss">
.nameTil {
  padding: 0;
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: 600;
}
.nameTill {
  padding: 0;
  margin: 10px 0;
  font-size: 16px;
  font-weight: 600;
}
</style>
